﻿using Framework.Utility.Mail;
using Microsoft.AspNetCore.Mvc;
using MonitorCode.DBCode;
using MonitorCode.Hangfire;
using MonitorCode.NPS;

namespace DevSupport.Areas.Mockup
{
    [ApiController]
    public class OnLineController : ControllerBase
    {
    
        private readonly INPSService _npsService;
        public OnLineController(INPSService npsService)
        { 
            _npsService = npsService;
        }
        /// <summary>
        /// 健康检查
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("api/HealthCheck")]
        public async Task<string> HealthCheck()
        {  
            //DBConnModel dBConnModel = new DBConnModel();
            //dBConnModel.DBType = "SqlServer";
            //dBConnModel.DBHostName= "localhost";
            //dBConnModel.DatabaseName= "DH";
            //dBConnModel.UserName= "sa";
            //dBConnModel.Password= "2023@ldh";

            //DBConnModel dB2 = new DBConnModel();
            //dB2.DBType = "MySql";
            //dB2.DBHostName = "127.0.0.1";
            //dB2.DatabaseName = "ldh";
            //dB2.UserName = "root";
            //dB2.Password = "123456";
            //dB2.port= 3306; 
            //string sql = "select * from biz_book";


            //var dbService = new DBFactory(dB2).DBService();
             

            //var ret = dbService.GetCountBySql(sql);

            return "Api is OK";
        }
        [HttpGet]
        [Route("api/HealthError")]
        public async Task<string> HealthError()
        {
            throw new Exception("抛出异常");
        }
        [HttpGet]
        [Route("api/Test/GetSqlData")]
        public async Task<string> GetSqlData()
        {
            string conn = "Data Source =.;Initial Catalog = DH;User id = sa;Password = 2023@ldh";
            string sql = "select * from Employee";
            var ret = _npsService.GetSqlTest(conn, sql);

            return "Api Data Count =" + ret;
        }
        [HttpGet]
        [Route("api/Test/GetMysqlData")]
        public async Task<string> GetMysqlData()
        {
            string conn = "server=127.0.0.1;userid=root;pwd=123456;port=3306;database=ldh;Allow User Variables=True;sslMode=None";
            string sql = "select * from biz_book";
            var ret = _npsService.GetMysqlTest(conn, sql);

            return "Api Data Count =" + ret;
        }
      /// <summary>
      /// 发邮件测试
      /// </summary>
      /// <param name="email">收件人</param>
      /// <param name="msg">内容</param>
      /// <returns></returns>
        [HttpGet]
        [Route("api/Test/sendEmail")]
        public string sendEmail(string email,string msg)
        {
            if (!email.IsEamil())
            {
                return  $"{email}邮件格式不正确";
            }
            var smtp = new SMTP("574427343@qq.com");
            var ccList = new string[] { "lee.li@mkcorp.com" };
            var resultE = smtp.Sendemail("测试邮件通知(系统邮件)", $"您好 {email}" + DateTime.Now.ToString(), ccList);
              
            return resultE ? "发送成功" : "发送失败";
        }
    }
}